package com.sydx.controller;


import com.sydx.entity.User;
import com.sydx.service.ClazzService;
import com.sydx.service.SubjectService;
import com.sydx.service.UserService;
import com.sydx.utils.MD5Utils;
import com.sydx.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    /*@Autowired
    private UserService userService;
*/
  /*  @PostMapping("/create")
    @ResponseBody
    private Map<String,Object> create(User user) {
// Map<String,Object> map = new HashMap<>();
        int result = userService.create(user);
        if (result <= 0) {
            // System.out.println("新增失败");
            return MapControl.getInstance().error().getMap();
        }
        // map.put("200", "新增成功");
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete{id}")
    @ResponseBody // 因为前端用的是layUI,所以返回的是json
    private Map<String,Object> delete(@PathVariable("id")Integer id) {
        int result = userService.delete(id);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        // map.put("200", "新增成功");
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/update")
    @ResponseBody
    private Map<String,Object> update(User user) {
        int result = userService.update(user);
        if (result <= 0) {
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/detail{id}")
    @ResponseBody
    private Map<String,Object> detail(@PathVariable("id")Integer id) {
        User user = userService.detail(id);
        if (user == null) {
            return MapControl.getInstance().nodata().getMap();// 没有数据
        }
        return MapControl.getInstance().success().put("data",user).getMap();
    }

    @PostMapping("/query")
    @ResponseBody
    private Map<String,Object> query(User user) {
        List<User> list = userService.query(user);

        return MapControl.getInstance().success().put("data",user).getMap();

    }*/
    private static final String LIST = "user/list";
    private static final String ADD = "user/add";
    private static final String UPDATE = "user/update";

    @Autowired
    private UserService userService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private ClazzService clazzService;

    @GetMapping("/add")
    public String create(ModelMap modelMap){
        return ADD;
    }

    @PostMapping("/create")
    @ResponseBody
    public Map<String,Object> create(@RequestBody User user){
        //加密
        user.setUserPwd(MD5Utils.getMD5(user.getUserPwd()));
        int result = userService.create(user);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = userService.delete(id);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = userService.delete(ids);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody User user){
        int result = userService.update(user);
        if(result<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap){
        User user = userService.detail(id);
        modelMap.addAttribute("user",user);
        return UPDATE;
    }

    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(@RequestBody User user){
        List<User> list = userService.query(user);
        Integer count = userService.count(user);
        return MapControl.getInstance().success().page(list,count).getMap();
    }

    @GetMapping("/list")
    public String list(){
        return LIST;
    }


}
